package Util;

import UI.SWTUtil;

/**
 * Class built for easy multi-threading.
 * Users of this class must implement three methods: onPre(), onPerform(), onPost()
 * The methods are performed in the order: onPre, onPerform, onPost
 * onPre() and onPost() are performed on the UI thread, while onPerform() is run on
 * a different thread.
 */
public abstract class AsyncTask
{
	public void start()
	{
		SWTUtil.getDisplay().asyncExec(new Runnable()
		{
			@Override
			public void run()
			{
				onPre();
				
				new Thread(new Runnable()
				{
					@Override
					public void run()
					{
						onPerform();
						
						SWTUtil.getDisplay().asyncExec(new Runnable()
						{
							@Override
							public void run()
							{
								onPost();
							}
						});
					}
				}).start();
			}
		});
	}
	
	/**
	 * Code to be executed on the UI thread, before all other code
	 */
	protected abstract void onPre();
	
	/**
	 * Code to be executed on a non-UI thread. Here the main code of the task should lie.
	 */
	protected abstract void onPerform();
	
	/**
	 * Code to be executed on the UI thread, after all other code.
	 */
	protected abstract void onPost();
}
